using System;
using IStrong.EC.Abstractions.Interfaces;
using IStrong.EC.DAO.Abstractions.Interfaces;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Linq;
using IStrong.EC.ExtensionMethods;
using NPOI.SS.UserModel;
using System.IO;
using NPOI.HSSF.UserModel;
using Newtonsoft.Json.Linq;
using Microsoft.AspNetCore.Hosting;
using System.Collections;
using System.Collections.Generic;
using IStrong.EC.WebApi.Results.Entity;

namespace IStrong.EC.WebApi.Results.Repository
{
    /// <summary>
    /// 数据访问类
    /// </summary>
    public class Utility : IService
    {
        /// <summary>
        /// 数据库操作组件
        /// </summary>
        private readonly IDbContext _db;

        /// <summary>
        /// 配置信息
        /// </summary>
        private readonly IConfiguration _configuration;

        /// <summary>
        /// 日志组件
        /// </summary>
        private readonly ILogger<Utility> _logger;


        /// <summary>
        /// 
        /// </summary>
        public readonly IHostingEnvironment _hostingEnvironment;
        /// <summary>
        /// 注入服务
        /// </summary>
        /// <param name="db"></param>
        /// <param name="configuration"></param>
        /// <param name="hostingEnvironment"></param>
        /// <param name="logger"></param>
        public Utility(IDbContext db, ILogger<Utility> logger, IConfiguration configuration, IHostingEnvironment hostingEnvironment)
        {
            _db = db;
            _logger = logger;
            _configuration = configuration;
            _hostingEnvironment = hostingEnvironment;
        }

        #region 导出方法

        /// <summary>
        /// 
        /// </summary>
        /// <param name="titleConfig"></param>
        /// <param name="fieldConfig"></param>
        /// <param name="data"></param>
        /// <param name="titel"></param>
        /// <returns></returns>
        public string ExportCustomerToExcel(JToken titleConfig, JToken fieldConfig, JToken data, string titel)
        {
            var book = new HSSFWorkbook();
            ICellStyle cellStyle = book.CreateCellStyle();
            cellStyle.Alignment = HorizontalAlignment.Center;
            IFont font = book.CreateFont();
            font.FontHeightInPoints = 12;
            font.Boldweight = (short)FontBoldWeight.Bold;
            cellStyle.SetFont(font);

            ISheet sheet = book.CreateSheet("Sheet1");
            IRow drow = sheet.CreateRow(0);
            titleConfig.ForIndexEach((i, e1) =>
            {
                ICell cell = drow.CreateCell(i, CellType.String);
                cell.SetCellValue(e1.ToString());
                cell.CellStyle = cellStyle;
            });
            data.ForIndexEach((i, e1) =>
            {
                drow = sheet.CreateRow(i + 1);
                fieldConfig.ForIndexEach((j, e2) =>
                {
                    ICell cell = drow.CreateCell(j, CellType.String);
                    cell.SetCellValue(e1[e2.ToString()].Value<string>());
                });
            });
            fieldConfig.ForIndexEach((j, e2) =>
            {
                sheet.AutoSizeColumn(j, true);
            });
            //var saveRoot = TypeHelper.CreateInstance<FileSaveDirectoryConfig>().FileExportDirectory;
            var saveRoot = $"{_hostingEnvironment.ContentRootPath}{@"\"}Exprot{@"\"}{string.Format("{0:yyyyMMdd}", DateTime.Now)}{@"\"}";
            if (!Directory.Exists(saveRoot))
            {
                Directory.CreateDirectory(saveRoot);
            }
            string fileName = titel + string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now) + ".xls";
            fileName = Path.Combine(saveRoot, fileName);
            using (var fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
            {
                book.Write(fs);
            }
            return fileName;
        }
        #endregion 导出方法

        /// <summary>
        /// 合并数组
        /// </summary>
        /// <param name="First">第一个数组</param>
        /// <param name="Second">第二个数组</param>
        /// <returns>合并后的数组(第一个数组+第二个数组，长度为两个数组的长度)</returns>
        public string[] MergerArray(string[] First, string[] Second)
        {
            string[] result = new string[First.Length + Second.Length];
            First.CopyTo(result, 0);
            Second.CopyTo(result, First.Length);
            return result;
        }

        /// <summary>
        /// 获取行政区划数据
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public IEnumerable<Area> GetArea(object param)
        {
            return _db.Query<Area>("Site.s_area", param);
        }
    }
}
